package com.example.demo.leetcode.classics150;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _20最长公共前缀
 * @description
 * @date 2023/07/14 16:09
 * <p>
 * ******************************************************
 */
public class _20最长公共前缀 {

    public String longestCommonPrefix(String[] strs) {
        StringBuilder sb = new StringBuilder();

        int length = strs.length;
        char match = ' ';
        int index = 0;
        for (int i = 0; i < length; i++) {
            if (i == 0 && strs[i].length() > index) {
                match = strs[i].charAt(index);
            }
            if (strs[i].length() <= index || match != strs[i].charAt(index)) {
                break;
            }

            if (i == length - 1) {
                sb.append(match);
                index++;
                i = -1;
            }
        }

        return sb.toString();
    }


    public static void main(String[] args) {
        _20最长公共前缀 s = new _20最长公共前缀();
        System.out.println(s.longestCommonPrefix(new String[]{"flower", "flow", "flight"}));
    }
}
